<?php defined('SYSPATH') OR die('No direct access allowed.');

class SqlTemplate_Plugin extends WB_FormItem {
	
	protected $necessary_fields, $sql_expression;
	
	public function __construct($name='', $params=array())
	{
		parent::__construct($name, $params);
		
		$this->form_columns = null;
		$this->db_columns = null;
		$this->table_columns = isset($this->table_columns) ? $this->table_columns : null;
		
		// Database hack: Kohana system/libraries/Database.php @ select function (line 287? ) 
        $this->table_columns[$this->name] = isset($this->params['sql']) ? '!!'.$this->params['sql'].'!!' : null;
	}
	
	public function render_item() {return null;}

	
	/**
	 * Returns a select statement for "table" view
	 * 
	 * @return string
	 */
	public function get_table_select()
	{
		$select = array();
		$select[] = $this->params['sql_expression'].' AS `'.$this->name.'`';
		if (is_array($this->necessary_fields)) foreach ($this->necessary_fields as $name=>$col) $select[] = is_numeric($name) ? $col : $col.' AS `'.$name.'`';
		return implode(', ', $select);
	}
	
}

//End of SqlTemplate_Plugin Library